<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('./spider') // echarts theme
import { debounce } from '@/utils'

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    optionTitleText: {
      type: String,
      default: ''
    },
    optionSeriesData:{
      type: Array,
      default: []
    },
    optionLegend:{
      type: Array,
      default: []
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null,
      opinion:{
        title: {
          text: this.optionTitleText,
          padding: 10,
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'line',
            lineStyle: {
              color: 'rgba(0,0,0,0.2)',
              width: 1,
              type: 'solid'
            }
          }
        },
        toolbox: {
          feature: {
            saveAsImage: {}
          }
        },
        legend: {
          data: this.optionLegend
        },
        grid: {
          top: 30,
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        singleAxis: {
          top: 50,
          bottom: 50,
          axisTick: {},
          "axisLabel":{
            interval: 0
          },
          type: 'time',
          axisPointer: {
            animation: true,
            label: {
              show: true
            }
          },
          splitLine: {
            show: true,
            lineStyle: {
              type: 'dashed',
              opacity: 0.2
            }
          }
        },

        series: [
          {
            type: 'themeRiver',
            emphasis: {
              itemStyle: {
                shadowBlur: 20,
                shadowColor: 'rgba(0, 0, 0, 0.8)'
              }
            },
            data: this.optionSeriesData
          }
        ]
      }
    }
  },
  mounted() {
    this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  watch:{
    optionSeriesData(newValue,oldValue){
      console.log("newValue",JSON.stringify(newValue))
      this.opinion.series[0].data=newValue
      this.chart.setOption(this.opinion);
    },
    optionLegend(newValue,oldValue){
      this.opinion.legend.data=newValue
      this.chart.setOption(this.opinion);
    },
    optionTitleText(newValue,oldValue){
      this.opinion.title.text=newValue
      this.chart.setOption(this.opinion);
    },
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'spider')
      this.chart.setOption(this.opinion)
    }
  }
}
</script>
